<?php
require_once 'Response.php';

class ResponseByPage extends Response {
    private $total;
    private $currentPage;
    private $pageSize;
    private $totalPages;
    private $list;

    public function __construct($success = false, $message = '', $total = 0, $currentPage = 1, $pageSize = 10, $list = [], $errors = null) {
        parent::__construct($success, $message, null, $errors);
        $this->total = $total;
        $this->currentPage = $currentPage;
        $this->pageSize = $pageSize;
        $this->totalPages = ceil($total / $pageSize);
        $this->list = $list;
    }

    public static function success($message = '操作成功', $total = 0, $currentPage = 1, $pageSize = 10, $list = []) {
        return new self(true, $message, $total, $currentPage, $pageSize, $list, null);
    }

    public function toJson() {
        return json_encode([
            'success' => $this->success,
            'message' => $this->message,
            'data' => [
                'total' => $this->total,
                'current_page' => $this->currentPage,
                'page_size' => $this->pageSize,
                'total_pages' => $this->totalPages,
                'data' => $this->list
            ],
            'errors' => $this->errors
        ]);
    }
}
